package databasetest;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.BorderLayout;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.Vector;

import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class Select extends JInternalFrame {

	DatabaseTest d;
	
	Connection conn;
	DefaultTableModel model;
	Vector<String> v;

	private JPanel panel;
	private JTextField textField;
	private JButton btnNewButton;
	private JScrollPane scrollPane;
	private JTable table;

	/**
	 * Create the frame.
	 */
//	public Select(Connection conn, Update up, Delete del, Insert in) {
	public Select(Connection conn, DatabaseTest d) {

		super("Select", false, true, false, false);// 두번째꺼 닫기창
		this.conn = conn;

		this.d=d;
		
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setBounds(450, 0, 450, 300);

		getContentPane().add(getPanel(), BorderLayout.NORTH);
		getContentPane().add(getScrollPane(),BorderLayout.CENTER);
	}

	
	// 쿼리 검색
	public void QueryStart(){

		String sql = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		ResultSetMetaData rsmd = null;
		int colCount = 0;
		v = new Vector<String>();

		if (scrollPane != null) {
			model.setColumnCount(0); //컬럼 지움
			model.setNumRows(0);	 //정보 지움
		}

		try {

			sql = textField.getText();
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();

			// 리절트셋에 대한 부가정보를 갖는 객체 얻어오기
			rsmd = pstmt.getMetaData();

			// 컬럼 갯수 구하고 갯수만큼 for문을 통해서 벡터에 추가 !
			colCount = rsmd.getColumnCount();
			for (int i = 1; i <= colCount; i++) {
				String colName = rsmd.getColumnName(i);
				v.add(colName);
			}
			
			//다시 테이블에 뿌려줌!!!
			scrollPane.setViewportView(getTable());

			while (rs.next()) {
				Vector<String> vv = new Vector<String>();

				for (int i = 1; i <= colCount; i++) {
					vv.add(rs.getString(i));
				}
				model.addRow(vv);
			}
			

			rs.close();
			pstmt.close();

		} catch (Exception ee) {
		}
	}
	
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getTextField(), BorderLayout.CENTER);
			panel.add(getBtnNewButton(), BorderLayout.EAST);

		}
		return panel;
	}

	private JTextField getTextField() {
		if (textField == null) {
			textField = new JTextField();
			textField.addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent e) {
					if (e.getKeyCode()==KeyEvent.VK_ENTER ){
						QueryStart();
					}
				}
			});
			textField.setColumns(10);
		}
		return textField;
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("쿼리실행");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					QueryStart();
				}
			});
		}
		return btnNewButton;
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}

	private JTable getTable() {
		// if (table == null) {
		model = new DefaultTableModel(v, 0) {
			public boolean isCellEditable(int row, int column) { // 테이블에서 수정
																	// 안되게함~
				return false;
			}
		};
		table = new JTable(model);
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getButton() == 1) { 
					int q = table.getSelectedRow();
					
					Vector<String> vec = new Vector<String>();
					
					vec.add((String)table.getValueAt(q, 0));
					vec.add((String)table.getValueAt(q, 1));
					vec.add((String)table.getValueAt(q, 2));
					
					String a =vec.get(0);
					String b =vec.get(1);
					String c =vec.get(2);
					
//					d.up.textCode.setText(a);
//					d.up.textEa.setText(b);
//					d.up.textSerial.setText(c);
//					
//					d.del.textCode.setText(a);
//					d.del.textEa.setText(b);
//					d.del.textSerial.setText(c);
					d.up.setData(a, b, c);
					d.del.setData(a, b, c);
//					
//					d.up.setD(model, table, q);
//					d.del.setD(model, table, q);
					
					d.up.setVisible(true);
					d.del.setVisible(true);
					
				}
			}
		});
		// }
		return table;
	}
}
